package cn.lliiooll.cmd;

import cn.lliiooll.util.Utils;
import cn.lliiooll.config.Config;
import cn.lliiooll.config.SubConfig;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.command.defaults.HelpCommand;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lliiooll
 * 用于处理/help、/?指令
 */
public class HelpCmd implements TabExecutor {
    //;
    @Override
    public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
        if (!(sender instanceof Player) && Config.console) {// 判断发送者不为玩家且开启了控制台不屏蔽
            new HelpCommand().execute(sender, s, args);// 转到原指令处理器
            return true;// 返回原版帮助
        }
        StringBuilder sb = new StringBuilder();
        if (args.length < 1) {
            // 无args
            sb = printLines(1, Config.list);
        } else {
            if (Utils.isNumeric(args[0])) {
                sb = printLines(Integer.parseInt(args[0]), Config.list);
            } else {
                // 子指令
                if (Config.hasSub(args[0])) {
                    // 存在
                    SubConfig sub = Config.getSub(args[0]);
                    if (args.length < 2) {
                        // 无args
                        sb = printLines(1, sub.list);
                    } else {
                        if (Utils.isNumeric(args[1])) {
                            sb = printLines(Integer.parseInt(args[1]), sub.list);
                        } else {
                            sb.append("&2未知的子指令");
                            return true;
                        }
                    }
                } else {
                    sb.append("§e未知的指令.");
                }
            }
        }
        sender.sendMessage(Utils.isPlayer(sender) ? PlaceholderAPI.setPlaceholders((Player) sender, sb.toString().replace("&", "§")) : sb.toString().replace("&", "§"));
        return true;
    }

    private StringBuilder printLines(int page, List<String> list) {
        String head = Config.head;
        String last = Config.last;
        StringBuilder sb = new StringBuilder();
        int pa = page - 1;
        if (pa < 0) {
            sb.append("§2页数不可为").append(page).append("~");
        } else {
            sb.append(head.replace("%now%", page + "")
                    .replace("%all%", ((list.size() / Config.line) + 1 + "").split("\\.")[0])).
                    append("\n");
            for (int i = Config.line * pa; i < Config.line * pa + Config.line; i++) {
                if (i >= list.size()) break;
                sb.append(list.get(i)).append("\n");
            }
            sb.append(last.replace("%now%", page + "")
                    .replace("%all%", ((list.size() / Config.line) + 1 + "").split("\\.")[0]));

        }
        return sb;
    }

    @Override
    public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {
        if (args.length < 1) return Config.getAllSub();
        return new ArrayList<>();
    }
}